import java.util.Arrays;

/**
 * @author wangj881
 * @date 2024-11-18 17:22
 */
public class L27_2 {

    public static void main(String[] args) {
        int[] nums = {4, 5};
        int val = 5;
        int i = new Solution().removeElement(nums, val);
        System.out.println(i);
    }

//    static class Solution {
//        public int removeElement(int[] nums, int val) {
//            int left = 0;
//            int right = nums.length - 1;
//            while (left <= right  && right >= 0) {
//                while (nums[left] == val && right >= 0) {
//                    nums[left] = nums[right];
//                    nums[right] = val;
//                    right--;
//                }
//                left++;
//            }
//            return right + 1;
//        }
//    }

    static class Solution {
        public int removeElement(int[] nums, int val) {
            int left = 0;
            int right = nums.length - 1;
            while (left <= right) {
                if (nums[left] == val) {
                    nums[left] = nums[right];
                    right--;
                } else {
                    left++;
                }
            }
            return left;
        }
    }
}
